﻿using System;
using System.ComponentModel;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;

namespace SetAnonymousListOffice365.SetAnonymousWebPart
{
    [ToolboxItem(false)]
    public partial class SetAnonymousWebPart : System.Web.UI.WebControls.WebParts.WebPart
    {
        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
            InitializeControl();
        }

        protected void Page_Load(object sender, EventArgs e)
        {
        }

        protected void btnGo_Click(object sender, EventArgs e)
        {

            lblerror.Text = "";

            SPList list = SPContext.Current.Web.Lists[txtListName.Text];

            string[] Anonlist = list.AnonymousPermMask64.ToString().Split(',');

            foreach(string s in Anonlist)
            {
                if (s.TrimStart() == "ViewListItems")
                {
                    chkViewListItems.Checked = true;
                }
                else if (s.TrimStart() == "AddListItems")
                {
                    chkAddListItems.Checked = true;
                }
                else if (s.TrimStart() == "OpenItems")
                {
                    chkOpenItems.Checked = true;
                }
                else if (s.TrimStart() == "ViewFormPages")
                {
                    chkViewFormPages.Checked = true;
                }
                else if (s.TrimStart() == "Open")
                {
                    chkOpen.Checked = true;
                }
            }

            lblerror.Text = "List : " + txtListName.Text;
            
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            // get the "Comments" list
            try
            {
                SPList list = SPContext.Current.Web.Lists[txtListName.Text];

                // check if it has unique permissions
                if (!list.HasUniqueRoleAssignments)
                {
                    list.BreakRoleInheritance(true);
                }

                // make sure people can edit their own items
                list.WriteSecurity = 2;

                SPBasePermissions permissions = SPBasePermissions.EmptyMask;

                if (chkOpen.Checked)
                    permissions ^= SPBasePermissions.Open;

                if (chkOpenItems.Checked)
                    permissions ^= SPBasePermissions.OpenItems;

                if (chkViewFormPages.Checked)
                    permissions ^= SPBasePermissions.ViewFormPages;

                if (chkViewListItems.Checked)
                    permissions ^= SPBasePermissions.ViewListItems;

                if (chkAddListItems.Checked)
                    permissions ^= SPBasePermissions.AddListItems;

                // grant permissions to anonymous users
                list.AnonymousPermMask64 = permissions;
                
                list.Update();

                lblerror.Text = "Save Successfully";

                txtListName.Text = "";
                chkOpen.Checked = false;
                chkOpenItems.Checked = false;
                chkViewFormPages.Checked = false;
                chkViewListItems.Checked = false;
                chkAddListItems.Checked = false;

            }
            catch (Exception ex)
            {
                lblerror.Text = ex.Message;
            }
        }
    }
}
